package com.heima.ai.config;

import cn.hutool.ai.Models;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.heima.ai.constants.SystemConstants;
import com.heima.ai.tool.CourseTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ai.vectorstore.SearchRequest;

import static com.heima.ai.constants.SystemConstants.CUSTOMER_SERVICE_SYSTEM;

@Configuration
public class CommonConfiguration {




    @Bean
    public ChatClient ollamaClient(OllamaChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model) // 创建ChatClient工厂
//                .defaultSystem("你是黑马程序员的前台人员小美,你需要以温柔可爱的语气回答客户的问题") // 设置系统提示词
                .defaultAdvisors(new SimpleLoggerAdvisor()) // 添加默认的Advisor,记录日志
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory)) //添加会话记录日志
                .build(); // 构建ChatClient实例
    }
    // 注意参数中的model就是使用的模型，这里用了OpenAIChatModel
    @Bean
    public ChatClient chatClient(DashScopeChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model) // 创建ChatClient工厂
                .defaultSystem("你是黑马程序员的前台人员小美,你需要以温柔可爱的语气回答客户的问题") // 设置系统提示词
                .defaultAdvisors(new SimpleLoggerAdvisor()) // 添加默认的Advisor,记录日志
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory)) //添加会话记录日志
                .build(); // 构建ChatClient实例
    }
    // 创建一个ChatMemory实例
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }
    //向量存储对象
    @Bean
    public VectorStore vectorStore(DashScopeEmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel).build();
    }
    
    @Bean
    public ChatClient gameChatClient(DashScopeChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                .defaultSystem(SystemConstants.GAME_SYSTEM_PROMPT)
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        new MessageChatMemoryAdvisor(chatMemory)
                )
                .build();
    }
    //智能客服客户端
    @Bean
    public ChatClient serviceChatClient(DashScopeChatModel model, ChatMemory chatMemory, CourseTool courseTool) {
        return ChatClient.builder(model)
                .defaultSystem(CUSTOMER_SERVICE_SYSTEM) //系统提示词
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(), //日志打印
                        new MessageChatMemoryAdvisor(chatMemory)// 会话记忆
                )
                .defaultTools(courseTool)//function Tool
                .build();
    }

    @Bean
    public ChatClient pdfChatClient(DashScopeChatModel model, ChatMemory chatMemory, VectorStore vectorStore) {
        return ChatClient.builder(model)
                .defaultSystem("请根据提供的上下文回答问题，不要自己猜测。")
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory), //会话记忆
                        new SimpleLoggerAdvisor(),//日志打印
                        new QuestionAnswerAdvisor(
                                vectorStore, // 向量库
                                SearchRequest.builder() // 向量检索的请求参数
                                        .similarityThreshold(0.5d) // 相似度阈值
                                        .topK(2) // 返回的文档片段数量
                                        .build()
                        )
                )
                .build();
    }
}